package test;

import com.medreams.dao.OrderDao;
import com.medreams.domain.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class OrderCURD {

    private InputStream resourceAsStream;
    private SqlSessionFactory buildFactory;
    private  SqlSession sqlSession;
    private OrderDao mapper;

    @Before
    public void before() throws IOException {
        resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        buildFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);//创建工厂并实例化一个类
        sqlSession = buildFactory.openSession();//开启事务
        mapper = sqlSession.getMapper(OrderDao.class);

    }

    @After
    public void after() throws IOException {
        sqlSession.commit();//提交事务
        sqlSession.close();//关闭实例
        resourceAsStream.close();//关闭读取文件的资源
    }

    @Test
    public void Select(){
        List<Order> users = mapper.orderList();
        for (Order user : users) {
            System.out.println(user);
            System.out.println(user.getUser());
        }
    }


}
